#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QVariant>
#include <QString>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    //对原始类型进行封装
    ui->setupUi(this);
    int value = data_plus(10 , 30).toInt();
    QString str = data_plus("hello ","world").toString();
    qDebug() <<value;
    qDebug() <<str;

    //创建Person对象
    Person p ;
    p.id = 250;
    p.name = "zzzzio";

#if 0
    QVariant v;
    v.setValue(p);

#else
    QVariant v = QVariant::fromValue(p);

#endif

    //取出v对象的数据
    if(v.canConvert<Person>())
    {
        Person tmp = v.value<Person>();
        qDebug() <<"id:"<<tmp.id<<",name:"<<tmp.name;
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
//原始类型封装函数
QVariant MainWindow::data_plus(QVariant a,QVariant b)
{
    QVariant return_value;
    if(a.type() == QVariant::Int && b.type() == QVariant::Int)
    {
        return_value = QVariant(a.toInt() + b.toInt());
    }
    else if(a.type() == QVariant::String && b.type() ==  QVariant::String);
    {
        return_value.setValue(a.toString()+b.toString());
    }
    return return_value;
}


